package com.example.myandtest;

import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentPagerAdapter;
import android.R.integer;
import android.R.xml;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a {@link FragmentPagerAdapter}
	 * derivative, which will keep every loaded fragment in memory. If this
	 * becomes too memory intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Create the adapter that will return a fragment for each of the three
		// primary sections of the activity.
		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager());

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		
		AsyncHttpClient client = new AsyncHttpClient();
		client.get("http://115.28.129.210:8000/api/matches", new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				
				try {
					Log.d("MyApp", response.getString("Step"));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				Log.d("MyApp", "http get recv");
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				
				Log.d("MyApp", "http get Start");
			}
			
			@Override 
			public void onFinish() {
				// TODO Auto-generated method stub
				super.onFinish();
				
				Log.d("MyApp", "http get Done");
			}
			
		});
		
		
		
	}
	
	public static void slideview(final float p1, final float p2, final View view) 
	{
		      TranslateAnimation animation = new TranslateAnimation(p1, p2, 0, 0);
		      animation.setInterpolator(new OvershootInterpolator());
		      animation.setDuration(1000);
		      animation.setStartOffset(0);
		      animation.setAnimationListener(new Animation.AnimationListener() {
		          @Override
		          public void onAnimationStart(Animation animation) {
		          }
		         
		         @Override
		         public void onAnimationRepeat(Animation animation) {
		         }
		         
		         @Override
		         public void onAnimationEnd(Animation animation) {
		             int left = view.getLeft()+(int)(p2-p1);
		             int top = view.getTop();
		             int width = view.getWidth();
		             int height = view.getHeight();
		             view.clearAnimation();
		             view.layout(left, top, left+width, top+height);
		         }
		     });
		     view.startAnimation(animation);
		 }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
	 * one of the sections/tabs/pages.
	 */
	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			// getItem is called to instantiate the fragment for the given page.
			// Return a PlaceholderFragment (defined as a static inner class
			// below).
			return PlaceholderFragment.newInstance(position + 1);
		}

		@Override
		public int getCount() {
			// Show 3 total pages.
			return 3;
		}
		

		@Override
		public CharSequence getPageTitle(int position) {
			Locale l = Locale.getDefault();
			switch (position) {
			case 0:
				return getString(R.string.title_section1).toUpperCase(l);
			case 1:
				return getString(R.string.title_section2).toUpperCase(l);
			case 2:
				return getString(R.string.title_section3).toUpperCase(l);
			}
			return null;
		}
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {
		/**
		 * The fragment argument representing the section number for this
		 * fragment.
		 */
		private static final String ARG_SECTION_NUMBER = "section_number";
		
		private DisplayImageOptions options;

		/**
		 * Returns a new instance of this fragment for the given section number.
		 */
		public static PlaceholderFragment newInstance(int sectionNumber) {
			PlaceholderFragment fragment = new PlaceholderFragment();
			Bundle args = new Bundle();
			args.putInt(ARG_SECTION_NUMBER, sectionNumber);
			fragment.setArguments(args);
			return fragment;
		}

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) 
		{
			View rootView = null;
			
			int pageIdx = getArguments().getInt(ARG_SECTION_NUMBER);
			if(pageIdx == 2)
			{
				rootView = inflater.inflate(R.layout.image_view, container,
						false);
				
				
				options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.defaultx)
				.showImageForEmptyUri(R.drawable.defaultx)
				.showImageOnFail(R.drawable.defaultx)
			//	.cacheInMemory(true)
				.cacheOnDisc(true)
				.considerExifParams(true)
				.build();
				
				
				ImageView iView = (ImageView)rootView.findViewById(R.id.imageView1);
				ImageLoader.getInstance().displayImage("http://cms.csdnimg.cn/article/201305/03/51834d562e039_middle.jpg", iView, options);
			
			}
			else 
			{
				rootView = inflater.inflate(R.layout.fragment_main, container,
						false);
				TextView textView = (TextView) rootView
						.findViewById(R.id.section_label);
				textView.setText(Integer.toString(getArguments().getInt(
						ARG_SECTION_NUMBER)));
				
				Button bt1 = (Button)rootView.findViewById(R.id.button1);
				bt1.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View arg0) 
					{
						// TODO Auto-generated method stub
						
						Intent it = new Intent();
						it.setClass(getActivity(), FragmenTabsTest.class);
						startActivity(it);
						
						 
						//slideview(0,100,arg0);
					}
				});
			}
			
			return rootView;
		}
	}

}
